<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
</head>
<body class="layui-layout-body">
<div class="layui-container" style="margin-top: 50px;width: 750px;">

    <div>
        搜索商品：
        <div class="layui-inline">
            <input class="layui-input" id="keyWord" autocomplete="off" lay-verify="required" placeholder="请输入商品名"/>
        </div>
        <button class="layui-btn" id="search" data-type="reload" >搜索</button>
    </div>

    <table class="layui-hide" id="test" style="width: 300px" lay-filter="test"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    <script type="text/javascript" th:src="@{/js/jquery-3.3.1.min.js}"></script>
    <script th:src="@{/layui/layui.js}" charset="utf-8"></script>
    <script>
        layui.use('table', function () {
            var table = layui.table;
            table.render({
                elem: '#test'
                , url: '/commodity/findAll'
                , title: '菜单列表'
                , id: 'testReload'
                , cols: [
                    [
                        {field: 'id', width: 100, title: '编号', sort: true}
                        , {field: 'name', width: 200, title: '商品'}
                        , {field: 'price', width: 100, title: '单价'}
                        , {field: 'count', width: 100, title: '数量'}
                        , {
                        field: 'tid', width: 100, title: '分类', templet: function (data) {
                            return data.type.name
                        }
                    }
                        , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 120}
                    ]
                ]
                , page: true
            });

            $("#search").on('click',function () {
                var name = $("#keyWord").val();
                if(name===''){
                    layer.msg('商品名不能为空',function() {time:2000});
                    return false;
                }
                console.log("name----"+name+"----");
                table.reload('testReload', {
                    url: '/commodity/findByName/'+name
                    , page: true
                });
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('确定要删除吗？', function (index) {
                        window.location.href = "/commodity/deleteById/" + data.id;
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    window.location.href = "/commodity/findById/" + data.id;
                }
            });

        });




    </script>

</div>
<script>
    //二级菜单联动
    layui.use('element', function () {
        var element = layui.element;

    });
</script>
</body>
</html>